// SPDX-License-Identifier: GPL-2.0-or-later
/* Driver for Virtio QTBox device.
 *
 * Copyright 2022 HUAWEI TECHNOLOGIES CO., LTD.
 */

#ifndef _VIRTIO_QTBOX_COMMON_H
#define _VIRTIO_QTBOX_COMMON_H

#include <linux/list.h>
#include <linux/virtio.h>
#include <linux/mutex.h>
#include "virtio_qtbox_sandbox.h"
#include "os_compat.h"

struct virtio_qtbox_dataq {
	struct virtqueue *vq;
	struct mutex dataq_mutex;
	char name[32];
};

struct virtio_qtbox {
	uint32_t max_dataqs;
	struct virtio_device *vdev;

	struct list_head node;

	struct mutex cmdq_mutex;
	struct virtqueue *cmdq;

	struct mutex eventq_mutex;
	struct virtqueue *eventq;
	struct workqueue_struct *event_wq;
	struct work_struct notify_work;

#define DATA_LOG_QUEUE             0
	struct virtio_qtbox_dataq *dataq;
	struct workqueue_struct *data_wq;
	struct work_struct notify_dataq_work;

	bool support_sandbox_vm;
	struct virtio_qtbox_sandbox sandbox;

	struct workqueue_struct *release_wq;
};

#define filp2sandbox(filp)	\
	container_of(filp->private_data, struct virtio_qtbox_sandbox, misc_dev)
#define sandbox2qtbox(ptr)	\
	container_of(ptr, struct virtio_qtbox, sandbox)

void sandbox_handle_event(struct virtio_qtbox *qtbox, uint32_t event);

int virtio_qtbox_sandbox_vm_init(struct virtio_qtbox_sandbox *sandbox);
int virtio_qtbox_sandbox_vm_uninit(struct virtio_qtbox_sandbox *sandbox);

int virtio_qtbox_do_cmd(struct virtio_qtbox *qtbox, uint32_t opcode, uint32_t subcode,
			void *para_flf, size_t flf_len, void *para_vlf, size_t vlf_len,
			void *result, size_t result_len);

void virtio_qtbox_event_cb(struct virtqueue *vq);
void virtio_qtbox_data_cb(struct virtqueue *vq);

void virtio_qtbox_event_work_handler(struct work_struct *work);
void virtio_qtbox_data_work_handler(struct work_struct *work);
uint32_t virtio_qtbox_eventq_fill(struct virtqueue *vq);
void virtio_qtbox_eventq_drain(struct virtqueue *vq);
uint32_t virtio_qtbox_dataq_fill(struct virtio_qtbox_dataq *dataq);
void virtio_qtbox_dataq_drain(struct virtio_qtbox *qtbox);

void sandbox_console_write_local(struct virtio_qtbox_sandbox_instance *instance,
			char *log, uint32_t log_len);
int virtio_qtbox_sandbox_console_init(struct virtio_qtbox_sandbox_instance *instance);
int virtio_qtbox_sandbox_console_uninit(struct virtio_qtbox_sandbox_instance *instance);

#endif
